import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

public class JsonToXMLTransformer extends AbstractTransformer {

	@Override
	protected Object doTransform(Object src, String enc)
			throws TransformerException {

		try {
			String srcString = (String) src;
			ObjectMapper mapper = new ObjectMapper();

			// 1. Retrieves our skills as objects list
			List<Skill> skills = mapper.readValue(
					srcString,
					TypeFactory.defaultInstance().constructCollectionType(
							List.class, Skill.class));

			// 2. Convert our skills to the skill output format
			COMPETENCES output = new COMPETENCES();
			for (Skill s : skills) {
				comPetence competence = new comPetence(s.getId(), s.getName());
				output.getCompetences().add(competence);
			}

			return output;
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;

	}
}
